package com.wiwf.utils;


import org.apache.hc.client5.http.classic.methods.HttpGet;
import org.apache.hc.client5.http.config.RequestConfig;
import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
import org.apache.hc.client5.http.impl.classic.CloseableHttpResponse;
import org.apache.hc.client5.http.impl.classic.HttpClientBuilder;
import org.apache.hc.client5.http.impl.classic.HttpClients;
import org.apache.hc.client5.http.impl.io.PoolingHttpClientConnectionManager;
import org.apache.hc.core5.http.io.SocketConfig;
import org.apache.hc.core5.http.io.entity.EntityUtils;
import org.apache.hc.core5.net.URIBuilder;

import java.util.concurrent.TimeUnit;

/**
 * @author kehui
 */
public class HttpClientUtil {

    private static CloseableHttpClient httpClient = null;

    private static final Integer SUCCESS_CODE = 200;

    static {
        //  创建连接池管理器
        PoolingHttpClientConnectionManager connManager = new PoolingHttpClientConnectionManager();

        connManager.setMaxTotal(1000);
        connManager.setDefaultMaxPerRoute(500);

        //  设置socket配置
        connManager.setDefaultSocketConfig(initSocketConfig().build());

        //  创建httpclient实例
        httpClient = initHttpClient(connManager);
    }

    /**
     * 初始化请求配置
     * @return
     */
    private static RequestConfig.Builder initRequestConfigBuilder() {
        RequestConfig.Builder custom = RequestConfig.custom();
        custom.setCookieSpec("default");
        custom.setConnectTimeout(30, TimeUnit.SECONDS);
        custom.setConnectionRequestTimeout(40, TimeUnit.SECONDS);
        return custom;
    }

    /**
     * 初始化socket配置
     * @return
     */
    private static SocketConfig.Builder initSocketConfig(){
        SocketConfig.Builder builder = SocketConfig.custom();
        builder.setTcpNoDelay(true);
        return builder;
    }

    /**
     * 初始化HttpClient
     * @return
     */
    public static CloseableHttpClient initHttpClient( PoolingHttpClientConnectionManager connManager) {
        HttpClientBuilder custom = HttpClients.custom();

        custom.setConnectionManager(connManager);
        custom.setDefaultRequestConfig(initRequestConfigBuilder().build());
        return custom.build();
    }

    /**
     * 获取HttpClient实例
     * @return
     */
    public static CloseableHttpClient getHttpClient() {
        return httpClient;
    }

    public static String sendGbkRequest(String urlParam, String requestType) {
        return sendBaseRequest(urlParam,requestType,"GBK");
    }
    public static String sendUtf8Request(String urlParam, String requestType) {
        return sendBaseRequest(urlParam,requestType,"UTF-8");
    }
    public static String sendBaseRequest(String urlParam, String requestType, String coding) {
        CloseableHttpClient httpClient = HttpClientUtil.getHttpClient();
        try {
            HttpGet httpGet = new HttpGet(new URIBuilder(urlParam).build());
            httpGet.setHeader("User-Agent","Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.198 Safari/537.36");

            CloseableHttpResponse response = httpClient.execute(httpGet);
            
            if(SUCCESS_CODE != response.getCode()){
                return "{}";
            }
            return EntityUtils.toString(response.getEntity(), "utf8");
        } catch (Exception e) {
            return "{}";
        }
    }


}
